package edu.uclmgmu.bacteriosec.aspects.java.io.OutputStream;

import java.io.OutputStream;

import edu.uclmgmu.bacteriosec.aspects.common.Store;

privileged aspect OutputStream_Duplication {

	void around(java.io.OutputStream caller, byte[] a, int b, int c) : target(caller) && args(a, b, c) &&  
			call(* OutputStream.write(byte[], int, int))  && 
			!cflow(within(OutputStream_Duplication)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			Store.updateNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				byte[] theArray=new byte[a.length + a.length];
				int i=0;
				for (i=0; i<a.length; i++) {
					theArray[i]=a[i];
				}
				for (int j=i; j<a.length+a.length; j++) {
					theArray[j]=a[j-i];
				}
				Store.log(thisJoinPointStaticPart.getSignature().toString() + "_" + this.getClass().getCanonicalName(),
						 numberOfCurrentCall, "<" + new String(a) + ">\n<" +
						new String(theArray) + ">");
				a=theArray;
			}
			proceed(caller, a, b, c);
		} else {
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a, b, c);
		}
	}

	void around(java.io.OutputStream caller, byte[] a) : target(caller) && args(a) &&  
			call(* OutputStream.write(byte[]))  && 
			!cflow(within(OutputStream_Duplication)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				byte[] theArray=new byte[a.length + a.length];
				int i=0;
				for (i=0; i<a.length; i++) {
					theArray[i]=a[i];
				}
				for (int j=i; j<a.length+a.length; j++) {
					theArray[j]=a[j-i];
				}
				a=theArray;
				Store.log(thisJoinPointStaticPart.getSignature().toString() + "_" + this.getClass().getCanonicalName(),
						 numberOfCurrentCall, "<" + new String(a) + ">\n<" +
						new String(theArray) + ">");
				Store.updateNumberOfCurrentCall(signature);
				proceed(caller, a);
			} else {
				Store.updateNumberOfCurrentCall(signature);
				proceed(caller, a);
			}
		} else {
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a);
		}
	}

	
}
